package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"video-hub/api/request"
	"video-hub/internal/bao"
	"video-hub/internal/mobel"
	"video-hub/pkg"
)

func VideoPush(context *gin.Context) {
	var video_push request.VideoPush
	if err := context.ShouldBind(&video_push); err != nil {
		context.JSON(http.StatusBadRequest, request.Ret{
			Code: 1000,
			Msg:  "参数解析错误",
			Data: err,
		})
		return
	}
	file, m, err := context.Request.FormFile("video")
	if err != nil {
		context.JSON(http.StatusBadRequest, request.Ret{
			Code: 1000,
			Msg:  "参数解析错误",
			Data: err,
		})
		return
	}
	upload, err := pkg.Upload(file, m)
	if err != nil {
		context.JSON(http.StatusBadRequest, request.Ret{
			Code: 1001,
			Msg:  "文件上传错误",
			Data: err,
		})
		return
	}
	var video mobel.Video
	video = mobel.Video{
		Hid:      video_push.Hid,
		Title:    video_push.Title,
		Des:      video_push.Des,
		VideoUrl: upload,
	}
	if err := bao.Create(&video); err != nil {
		context.JSON(http.StatusBadRequest, request.Ret{
			Code: 1002,
			Msg:  "剧集上传失败",
			Data: err,
		})
		return
	}
	context.JSON(http.StatusBadRequest, request.Ret{
		Code: 200,
		Msg:  "剧集上传成功",
		Data: video,
	})
}
